package com.longkey.workflow.client.control
{
	import com.longkey.workflow.client.model.TemplateModel;
	import com.longkey.workflow.client.util.IDGenerator;
	
	import org.longkey.portal.MaskUtil;
	import org.longkey.portal.Portal;

	public class Model
	{
		public function Model()
		{
			_tempMap = new Object();
		}
		
		private static var _instance:Model;
		
		public static function getInstance():Model{
			if(_instance == null){
				_instance = new Model();
			}
			return _instance;
		}
		
		private var _tempMap:Object;
		private var _cb:Function;
		
		public function loadTemplate(id:Number, callback:Function):void{
			this._cb = callback;
			var template:TemplateModel = getTemplate(id);
			if(template != null){
				this._cb(template);
			}
			else{
				template = new TemplateModel();
				template.templateId = id;
				template.templateKey = IDGenerator.UUID();
				template.templateName = "测试用流程";
				this._cb(template);
				//			var params:Object = {"templateId":id};
				//			Portal.execjava(onloadTemplate, "", params);
			}
		}
		
		private function onloadTemplate(data:Object):void{
			var template:TemplateModel = new TemplateModel(data);
			if(template.templateKey != null){
				_tempMap[template.templateId] = template;
				this._cb(template);
			}
			else{
				MaskUtil.error("加载流程失败");
			}
		}
		
		public function getTemplate(id:Number):TemplateModel{
			if(_tempMap.hasOwnProperty(id)){
				return _tempMap[id] as TemplateModel;
			}
			return null;
		}
		
		public function saveTemplate(id:Number):void{
			
		}
	}
}